/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.unicauca.opentextbook.controladores;

import java.sql.SQLException;
import javax.annotation.Resource;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.transaction.RollbackException;
import javax.transaction.UserTransaction;

/**
 *
 * @author Gamificacion
 */
public class BaseController {

    @Resource
    protected UserTransaction userTransaction;

    protected void anadirMensajeError(String mensaje) {
        FacesContext context = FacesContext.getCurrentInstance();
        context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, mensaje, null));
    }

    protected void anadirMensajeInfo(String mensaje) {
        FacesContext context = FacesContext.getCurrentInstance();
        context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, mensaje, null));
    }

    protected void anadirMensajeWarn(String mensaje) {
        FacesContext context = FacesContext.getCurrentInstance();
        context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, mensaje, null));
    }

    public void commitTransaction() throws SQLException {
        try {
            userTransaction.commit();
        } catch (RollbackException e) {
            throw new SQLException(e.getCause());
        } catch (Exception e) {
        }
    }
}
